package de.islanders.unit.player.inventory;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Iterator;
import java.util.Map;

//TODO Observer!
public class Encumbrance implements PropertyChangeListener {
	private double currentEncumbrance,
					maxEncumbrance;
	private final ItemHandler inventory;
	
	public Encumbrance(ItemHandler inventory) {
		this.inventory = inventory;
	}
	
	public double max() {
		return maxEncumbrance;
	}
	
	public double current() {
		return currentEncumbrance;
	}
	
	private void updateCurrent() {
		double newCurrent = 0d;
		Iterator<Map.Entry<Integer, InventoryItem>> itr = inventory.getInventory().entrySet().iterator();
		while(itr.hasNext()) {
			InventoryItem item = itr.next().getValue();
			newCurrent += item.getWeight();
		}
		currentEncumbrance = newCurrent;
	}
	
	private void updateMaximum() {
		double newMax = 0d;
	}

	@Override
	public void propertyChange(PropertyChangeEvent arg0) {
		if(arg0.getPropertyName().equalsIgnoreCase("bagItem()")) {
			System.out.println("bagItem() event!");
			updateCurrent();
		} else if(arg0.getPropertyName().equalsIgnoreCase("dropItem()")) {
			System.out.println("dropitem() event!");
			updateCurrent();
		}
	}	
}

/*
	maxEncumbrance
	- increases -- nothing happens
	- decreases -- check if encumbered
	currentEncumbrance
	- increases	-- check if encumbered
	- decreases	-- check if encumbrance state changes
*/